<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/2
 * Time: 10:53
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\UserCoupon;
use common\models\DAO\UserCouponUseList;

class CouponService {
	
    /*
	* 用户年利卷总数
	*/
    public static function get($userID){
        $now = date('Ymd');
        $time = strtotime($now);
        $list = [];
        $result = UserCoupon::find()->where(['userID'=>$userID])->andWhere("$now<loseTime and status=0")->orderBy('loseTime asc')->all();
        if($result){
            foreach($result as $v){
                $coupon['day'] =  (strtotime($v['loseTime']) - $time)/24/3600;
                $coupon['rate'] = CommonFunction::formatMoney($v['card']);
                $coupon['id'] = $v['id'];
                $list[] = $coupon;
            }
        }
        return $list;
    }

    public static function one($userID){
        $now = date('Ymd');
        return UserCoupon::find()->where(['userID'=>$userID])->andWhere("$now<loseTime and status=0")->orderBy('loseTime asc')->one();
    }

    /*
     * 获取年利卷信息
     */
    public static function getInfo($userID, $id){
        $coupon = UserCoupon::findOne(['id'=>$id, 'userID'=>$userID, 'status'=>0]);
        if(!$coupon)
            return false;
        $lose_time = $coupon['loseTime'];
        if(time() >= $lose_time )
            return false;
        return $coupon;
    }

    public static function getList($userID, $page = 1, $limit = 0){
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 10;
        $start = (intval($page) - 1) * $limit;
        $list = [];
        $result = UserCoupon::find()->where(['userID'=>$userID])
                  -> orderBy('status asc, loseTime desc')->limit($limit)->offset($start)->all();
        if($result){
            $today = date('Ymd');
            foreach($result as $v){
                $coupon['day'] =  date('m月d', strtotime($v['loseTime']));
                $coupon['rate'] = CommonFunction::formatMoney($v['card']);
                $coupon['id'] = $v['id'];
                $coupon['status'] = $v['status'];
                if($v['loseTime'] <= $today && $v['status'] != 1)
                    $coupon['status'] = 1;
                $list[] = $coupon;
            }
        }
        return $list;
    }
	
	//年化券信息总览
	public static function couponInit($userID)
	{
		//未使用
		$data['wei']=UserCoupon::find()->where(['userID'=>$userID,'status'=>0])->andWhere("loseTIme > ".date('Ymd'))->count();
		//已过期
		$data['guoqi']=UserCoupon::find()->where(['userID'=>$userID,'status'=>0])->andWhere("loseTIme <= ".date('Ymd'))->count();
		//已使用
		$data['yi']=UserCouponUseList::find()->where(['userID'=>$userID])->count();
		return $data;
	}
	
	//未使用
	public static function getListwei($userID, $page = 1, $limit = 0){
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 12;
        $start = (intval($page) - 1) * $limit;
        $list = [];
        $result = UserCoupon::find()->where(['userID'=>$userID,'status'=>0])->andWhere("loseTIme > ".date('Ymd'))
                  -> orderBy('status asc, loseTime asc')->limit($limit)->offset($start)->all();
        if($result){
            foreach($result as $v){
                $coupon['day'] =  date('Y.m.d', strtotime($v['loseTime']));
                $coupon['rate'] = CommonFunction::formatMoney($v['card']);
                $coupon['id'] = $v['id'];
                $coupon['status'] = $v['status'];
                $list[] = $coupon;
            }
        }
        return $list;
    }
	
	
	//已使用
	public static function getListyi($userID, $page = 1, $limit = 0){
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 12;
        $start = (intval($page) - 1) * $limit;
        $list = [];
        $result = UserCouponUseList::find()->where(['userID'=>$userID])
                  -> orderBy('useTime desc')->limit($limit)->offset($start)->all();
        if($result){
            foreach($result as $v){
                $coupon['day'] =  date('Y.m.d', intval($v['useTime']));
                $coupon['rate'] = CommonFunction::formatMoney($v['card']);
                $coupon['id'] = $v['id'];
                $list[] = $coupon;
            }
        }
        return $list;
    }
	
	
	
	//已过期
	public static function getListguoqi($userID, $page = 1, $limit = 0){
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 12;
        $start = (intval($page) - 1) * $limit;
        $list = [];
        $result = UserCoupon::find()->where(['userID'=>$userID,'status'=>0])->andWhere("loseTIme <= ".date('Ymd'))
                  -> orderBy('status asc, loseTime asc')->limit($limit)->offset($start)->all();
        if($result){
            foreach($result as $v){
                $coupon['day'] =  date('Y.m.d', strtotime($v['loseTime']));
                $coupon['rate'] = CommonFunction::formatMoney($v['card']);
                $coupon['id'] = $v['id'];
                $coupon['status'] = $v['status'];
                $list[] = $coupon;
            }
        }
        return $list;
    }
	
	
}